package com.xiaomili.halfsugar.fragment;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.xiaomili.halfsugar.R;
import com.xiaomili.halfsugar.adapter.RecyclerViewAdapter;
import com.xiaomili.halfsugar.bean.LatestBean;
import com.xiaomili.halfsugar.bean.LatestBean.DataBean.TopicBean;
import com.xiaomili.halfsugar.utils.OKHttpUtils;
import com.xiaomili.halfsugar.utils.OKHttpUtils.MyCallback;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * A simple {@link Fragment} subclass.
 */
public class ItemFragment extends Fragment {
    private static final String KEY = "params";
    @InjectView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    private String mUrl;
    private List<TopicBean> mDates;
    private Context mContext;
    private Handler mHandler = new Handler();
    private static final String TAG = "ItemFragment";
    private RecyclerViewAdapter adapter;

    public static ItemFragment newInstance(String url) {
        Bundle args = new Bundle();
        args.putString(KEY, url);
        ItemFragment fragment = new ItemFragment();
        fragment.setArguments(args);
        return fragment;
    }


    public ItemFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        mDates = new ArrayList<>();
        if (getArguments() != null) {
            mUrl = getArguments().getString(KEY);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_item, container, false);
        ButterKnife.inject(this, view);
//        TextView textView = new TextView(mContext);
//        textView.setText("asidh");
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        loadData();
    }

    private void loadData() {
        OKHttpUtils.newInstance(mContext).obtainDataAsyncByGet(mUrl, new MyCallback() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String result) {
                final LatestBean latestBean = new Gson().fromJson(result, LatestBean.class);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        Log.i(TAG, "topic: " + latestBean.data.topic.size());
                        mDates.addAll(latestBean.data.topic);
                        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
                        adapter = new RecyclerViewAdapter(mContext);
                        adapter.setData(mDates);
                        adapter.notifyDataSetChanged();
                        mRecyclerView.setAdapter(adapter);
                    }
                });
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}